﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Mono.Cecil.Metadata
{
	// Token: 0x0200010E RID: 270
	internal class StringHeap : Heap
	{
		// Token: 0x06000A59 RID: 2649 RVA: 0x00005BB1 File Offset: 0x00003DB1
		public StringHeap(byte[] data) : base(data)
		{
		}

		// Token: 0x06000A5A RID: 2650 RVA: 0x0002E8DC File Offset: 0x0002CADC
		public string Read(uint index)
		{
			bool flag = index == 0U;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				string text;
				bool flag2 = this.strings.TryGetValue(index, out text);
				if (flag2)
				{
					result = text;
				}
				else
				{
					bool flag3 = (ulong)index > (ulong)((long)(this.data.Length - 1));
					if (flag3)
					{
						result = string.Empty;
					}
					else
					{
						text = this.ReadStringAt(index);
						bool flag4 = text.Length != 0;
						if (flag4)
						{
							this.strings.Add(index, text);
						}
						result = text;
					}
				}
			}
			return result;
		}

		// Token: 0x06000A5B RID: 2651 RVA: 0x0002E958 File Offset: 0x0002CB58
		protected virtual string ReadStringAt(uint index)
		{
			int num = 0;
			int num2 = (int)index;
			for (;;)
			{
				bool flag = this.data[num2] == 0;
				if (flag)
				{
					break;
				}
				num++;
				num2++;
			}
			return Encoding.UTF8.GetString(this.data, (int)index, num);
		}

		// Token: 0x0400047E RID: 1150
		private readonly Dictionary<uint, string> strings = new Dictionary<uint, string>();
	}
}
